var express = require('express');
var router = express.Router();
var redis = require('redis');
var lineReader = require('line-reader');
var config = require('../routes/config');
var fs = require('fs');



function callend(req,res,datas_list){
    var datas=[];
    var nowarray = [];
    var oldarray=[];
    var oldarray1=[];
    for (var key in datas_list)
    {
        oldarray1 = oldarray;
        oldarray = nowarray;
        nowarray = [];
        var date = datas_list[key];
        if (date)
        {
            var o = {};
            o.date = key;
            o.numbers = [];
            var xxx = ["00","01","02","03","04","05","06","07","08","09","10","11", "12", "13", "14", "15","16","17","18","19","20","21","22","23"];
            for (var item in xxx)
            {
                var houritem = date[xxx[item]];
                var nowcount = 0;
                if (houritem)
                {
                    var nn = houritem.number / houritem.count;
                    var item1 = {
                        hour:item,
                        number:Math.ceil(nn),
                    }
                    nowcount = Math.ceil(nn);
                    o.numbers.push(item1);
                }
                else
                {
                    var item1 = {
                        hour:item,
                        number:Math.ceil(0),
                    }
                    o.numbers.push(item1);
                }

                var oooo={
                    x:xxx[item], // 横坐标小时
                    y:nowcount
                };
                nowarray.push(oooo);
            }
            datas.push(o);
        }
    }

    var heads=[
        "日期", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15","16","17","18","19","20","21","22","23"
    ]

    for (var ii in nowarray)
    {
        var old = oldarray[ii];
        if (old)
        {
            nowarray[ii] .y1 = old.y;
        }
        old = oldarray1[ii];
        if (old)
        {
            nowarray[ii] .y2 = old.y;
        }
    }

    res.render('online', {
        links:config.linkmenu,
        title: '在线统计' ,
        datas:datas,
        heads:heads,
        index:config.link.INDEX,
        nowarray:JSON.stringify(nowarray),
        login:req.session.login
    });
}
/* GET users listing. */
router.get('/', function(req, res, next) {
    if (0 && !req.session.login)
    {
        res.render('index',{
            links:config.linkmenu,
            index:config.link.INDEX
        });
        return;
    }
  /**
   * 访问online_num 文件获取当前同时在线数量
   * */
  var datas_list={}
  lineReader.eachLine(config.onlineFile, function(line, last) {
    var arr = line.split("-");
    if (arr.length != 3)
    {//callend(req,res,datas_list);
      console.log("readline error");
      return;
    }

    var date = arr[0];
    var hour =arr[1].substring(0,2);
    var number = parseInt(arr[2]);

    console.log(date+" "+hour + " " + number + " " + last);
    var data = datas_list[date];
    if (!data) {
        datas_list[date] = {};
        data = datas_list[date];
    }
    var hour_data = data[hour];
    if (!hour_data) {
        var o = {
            date: date,
            hour: hour,
            number: number,
            count: 1,
        };

        data[hour] = o;
        hour_data = data[hour]
    }
    else
    {
        hour_data.count = hour_data.count + 1;
        hour_data.number += number;
    }
    if (last)
    {
        console.log(JSON.stringify(hour_data));
        callend(req,res,datas_list);
    }
  });

});

module.exports = router;
